<template>
  <div>
    <el-card class="margin_30" shadow="always" :body-style="{padding:'20px'}">
      <div slot="header">
        <span>条件查询</span>
      </div>
      <el-form ref="form" :inline="true" :model="formData" class="demo-form-inline">
        <el-row :gutter="20">
          <el-col :span="6" :offset="0">
            <el-form-item label="作者">
              <el-input v-model="formData.author" placeholder="作者" style="width:100%" size="medium " />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="标题">
              <el-input v-model="formData.title" placeholder="标题" style="width:100%" size="medium " />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="0">
            <el-form-item label="编辑类型">
              <el-select v-model="formData.editorType" placeholder="编辑类型" style="width:100%" size="medium ">
                <el-option label="富文本" value="0" />
                <el-option label="MarkDown" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" :offset="18">
            <el-button size="mini" @click="resetForm">重置</el-button>
            <el-button size="mini" type="primary" @click="onSubmit">搜索</el-button>
          </el-col>
        </el-row>

      </el-form>
    </el-card>
    <el-card class="box-card margin_30">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="mini" @click="goAddArticle">新增</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="list"
        border
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column
          label="#"
          align="center"
          width="50"
          type="index"
          fixed="left"
        />
        <!-- prop需要展示的数据部分 -->
        <!-- label表头的名称 -->
        <el-table-column
          prop="title"
          label="文章标题"
          align="center"
          width="200"
        />

        <el-table-column
          prop="coverImg"
          label="展示图片"
          width="200"
          align="center"
        >
          <!-- 作用域插槽 以前叫slot-scope 最新v-slot-->
          <template slot-scope="scope">
            <img style="width:100px;height:100px;" :src="scope.row.coverImg" alt="">
          <!-- {{ scope.row.coverImg }} -->
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="添加时间"
          align="center"
          width="250"
        />
        <el-table-column
          prop="author"
          label="文章作者"
          align="center"
          width="200"
        />
        <el-table-column
          prop="isShow"
          label="文章是否显示"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isShow"
              :active-value="1"
              :inactive-value="0"
              @change="changeShow(scope.row.isShow,scope.row.id)"
            />
          </template>
        </el-table-column>

        <el-table-column
          prop="editorType"
          label="内容"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.editorType == 0" type="primary" plain size="mini">富文本</el-button>
            <el-button v-else type="success" plain size="mini">MarkDown</el-button>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" fixed="right" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-view" @click="goEdit(scope.row.id)">编辑文章</el-button>
            <el-button style="color:red;" size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="paginationParams.start"
        :page-sizes="paginationParams.pageSize"
        :page-size="paginationParams.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="paginationParams.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

  </div>
</template>

<script>
import { findArticles, changeShowStatus, delArticle } from '@/api/content/article'
import mix from '@/mixins'
export default {
  mixins: [mix],
  data() {
    return {
      // 是否显示加载中
      loading: false,
      formData: {
        'author': '',
        'collectCount': '',
        'content1': '',
        'content2': '',
        'coverImg': '',
        // 中后台项目 不需要传递事件 如果需要传递事件 事件格式固定
        // 年-月-日 时：分：秒  2022-10-31 10：22：00
        // new Data().getFullYear()-new Data().getMonth()-new Data().getDay()  ...
        'createTime': '',
        'editorType': '',
        'id': '',
        'isShow': '',
        'modifyTime': '',
        'summary': '',
        'title': '',
        'viewCount': '',
        'zanCount': ''
      },
      list: []
    }
  },

  mounted() {
    this.init()
  },

  methods: {
    // 编辑文章
    goEdit(id) {
      this.$router.push(`/content/editArticle?id=${id}`)
    },
    // 新增文章
    goAddArticle() {
      this.$router.push({ name: 'contentaddArticle' })
    },
    // 删除文章
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(111)
        // 删除文章
        delArticle(id)
          .then(res => {
            console.log(res)
            if (res.success) {
              this.init()
            } else {
              this.$message.error(res.message)
            }
          })
      }).catch(() => {
        this.$message.info('你已取消删除操作')
      })
    },
    // 条件搜索
    onSubmit() {
      this.start = 1
      this.init()
    },
    // 重置表单
    resetForm() {
      this.start = 1
      this.formData = {}
      this.init()
    },
    // 文章显示状态开关修改---报错
    changeShow(isShow, id) {
      changeShowStatus({ isShow, id })
        .then(res => {
          if (res.success) {
            this.$message.success('文章显示状态修改成功')
          } else {
            this.$message.error(res.message)
          }
        })
    },
    // 获取列表
    init() {
      this.loading = true
      var searchObj = {}
      for (const key in this.formData) {
        if (this.formData[key] !== '') {
          searchObj[key] = this.formData[key]
        }
      }
      // console.log(searchObj)
      findArticles(this.paginationParams.start, this.paginationParams.limit, searchObj)
        .then(res => {
          console.log(res)
          this.list = res.data.rows
          this.paginationParams.total = res.data.total
          // this.list.forEach(ele => {
          //   ele.isShow = !!ele.isShow
          // })
          this.loading = false
        })
    },
    // 每页多少条改变时触发（main.js中切换页码中文）
    handleSizeChange(e) {
      this.paginationParams.limit = e
      this.init()
    },
    // 当前页码发生改变触发
    handleCurrentChange(e) {
      this.paginationParams.start = e
      this.init()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
